﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NLBS.Data;
using NLBS.Security;

namespace NLBS.Control
{
    public partial class panelUser : BaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PageInit();
        }

        private void PageInit()
        {
            bool login = Request.IsAuthenticated;
            loginPanel.Visible = !login;
            loginSuccessPanel.Visible = login;
            if (login)
            {
                loginName.Text = BlogInfo.CurrentUser.UserName;
            }
            //watercode
            securityCodePanel.Visible = BlogInfo.EnableSecurityCode.Equals(1);
            //register link
            registerPanel.Visible = BlogInfo.EnableRegister.Equals(1);
            //admin link
            adminPanel.Visible = BlogInfo.CurrentUser.GroupID.Equals(1);
            if (Power.Post <= 1)
                postPanel.Visible = false;
            Login.ServerClick += new EventHandler(Login_ServerClick);
        }

        #region event

        void Login_ServerClick(object sender, EventArgs e)
        {
            bool result = DbProvider.Instance().ValidateUser(username.Value, StringSecurity.StringToSHA1Hash(password.Value));
            if (result)
            {
                FormsAuthentication.SetAuthCookie(username.Value,true);
                Response.Redirect("default.aspx");
            }
            else
            {
                Response.Redirect("login.aspx");
            }
        }

        #endregion
    }
}